//
//  GoodsClassifyController.swift
//  XiaoJingMallSwift
//
//  Created by beisheng03 on 2017/7/21.
//  Copyright © 2017年 beisheng. All rights reserved.
//

import UIKit
import SVProgressHUD

class GoodsClassifyController: BaseViewController {
    
    var dataModels = [ClassifyModel]()
    
    
    var rightTabelModel : ClassifyModel?
    
    

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setNav()
        
        view.backgroundColor = UIColor.white
        
        automaticallyAdjustsScrollViewInsets = false
        
        loadData()
        
        setupTableView()
        
               // Do any additional setup after loading the view.
    }
    
    
    func setNav() {
        
        let bgView = NavTittleView(frame: CGRect(x: 0, y: 0, width: KScreenW, height: 44))
        
        bgView.backgroundColor = UIColor.clear
        
        let leftBtn = LightDisableBtn(type: .custom)
        
        leftBtn.frame = CGRect(x: 0, y: 0, width: 38, height: 44)
        
        leftBtn.setImage(UIImage(named: "ico_camera_7"), for: .normal)
        
        leftBtn.setImage(UIImage(named: "ico_camera_7"), for: .highlighted)
        
        leftBtn.backgroundColor = UIColor.clear
        
        leftBtn.addTarget(self, action: #selector(backBtnDidTap), for: .touchUpInside)
        
        bgView.addSubview(leftBtn)
        
        let btn = LightDisableBtn(type: .custom)
        
        btn.frame = CGRect(x: 40, y: 6, width: KScreenW - 80, height: 32)
        
        btn.setBackgroundImage(UIImage(named: "bg_search_bar_7_gray"), for: .normal)
        
        btn.addTarget(self, action: #selector(tittleBtnDidTap), for: .touchUpInside)
        
        btn.backgroundColor = UIColor.clear
        
        bgView.addSubview(btn)
        
        let label = UILabel()
        
        label.text = seachStr
        
        label.font = UIFont.systemFont(ofSize: 14)
        
        label.textColor = UIColor.colorWith(r: 145, g: 145, b: 145)
        
        label.sizeToFit()
        
        label.frame = CGRect(x: 30, y: 0, width: label.frame.size.width, height: 32)
        
        label.isUserInteractionEnabled = false
        
        btn.addSubview(label)
        
        let rightBtn = LightDisableBtn(type: .custom)
        
        rightBtn.frame = CGRect(x: KScreenW - 40, y: 0, width: 40, height: 44)
        
        rightBtn.setImage(UIImage(named: "JDMainPage_icon_message_18x18_"), for: .normal)
        
        rightBtn.setImage(UIImage(named: "JDMainPage_icon_message_18x18_"), for: .highlighted)
        
        rightBtn.backgroundColor = UIColor.clear
        
        rightBtn.addTarget(self, action: #selector(rightBtnDidTap), for: .touchUpInside)
        
        bgView.addSubview(rightBtn)
        
        navigationItem.titleView = bgView
        
    }
    
    
    func backBtnDidTap(){
        
        
        QL1("左边点击了")
        
    }
    
    
    func tittleBtnDidTap(){
        
        let tmepV = SearchController()
        
        
        let nav = BaseNavController(rootViewController: tmepV)
        
        
        present(nav, animated: true, completion: nil)
    }
    
    func rightBtnDidTap(){
        
        QL1("右边点击了")
    }
    
    
    
    
    
    
    func setupTableView() {
        
        groupTableView.frame = CGRect(x: 0, y: 0, width: 80, height: KScreenH)
        
        groupTableView.contentInset = UIEdgeInsetsMake(64, 0, 49, 0);
        
        groupTableView.rowHeight = 55;
        
        groupTableView.tableFooterView = UIView()
        
        groupTableView.separatorColor = UIColor.clear
        
        groupTableView.register(UINib(nibName: StringFromClass(classType: LeftTableViewCell.self), bundle: nil), forCellReuseIdentifier: LeftTableViewCell.identifer())
        
        
        plainTableView.frame = CGRect(x: 82, y: 0, width: KScreenW - 82, height: KScreenH)
        
        plainTableView.contentInset = UIEdgeInsetsMake(64, 0, 49, 0);
        
        plainTableView.rowHeight = 100
        
        plainTableView.separatorStyle = .none
        
        plainTableView.tableFooterView = UIView()
        
        plainTableView.register(UINib(nibName: StringFromClass(classType: RightTableViewCell.self), bundle: nil), forCellReuseIdentifier: RightTableViewCell.identifer())
        
        plainTableView.register(UINib(nibName: StringFromClass(classType: RightTabelHeaderView.self), bundle: nil), forHeaderFooterViewReuseIdentifier: RightTabelHeaderView.identifer())
        
        groupTableView.isHidden = true
        plainTableView.isHidden = true

        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

   
}


extension GoodsClassifyController{
    
    func loadData() {
        
        SVProgressHUD.show(withStatus: "正在加载...")
        
        NetworkTools.shared.sendRequest(method: .GET_METHOD, pathStr: "mobile/index.php?c=goods_class&a=index", parameters: nil) {[weak self] (responseObject, error) in
            
            
            if error == nil {
                
                SVProgressHUD.dismiss()
                
                for dic in responseObject!["datas"]!["class_list"] as! [[String : AnyObject]]{
                    
                    let temp = ClassifyModel(dic: dic)
                    
                    if temp.child.count > 0{
                        
                        self!.dataModels.append(temp)
                        
                        
                    }
                    
                    
                }
                
                if self!.dataModels.count > 0{
                    
                    self!.rightTabelModel = self!.dataModels[0]
                    
                    self!.groupTableView.isHidden = false
                    
                    self!.plainTableView.isHidden = false
                    
                    self!.groupTableView.reloadData()
                    
                    self!.plainTableView.reloadData()
                    
                    self!.groupTableView.selectRow(at: IndexPath(row: 0, section: 0), animated: false, scrollPosition: .top)
                    
                }
                
                
                
            }else{
                
                
                
                SVProgressHUD.showError(withStatus: error?.domain)
                
                
            }
            
            
            
            
        }
        
        
    }
    
    
    
    
}



extension GoodsClassifyController{
    
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        
        if tableView == groupTableView {
            
            return 1
            
        } else {
            
            return rightTabelModel?.child.count ?? 0
            
        }
    }
    
    
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if tableView == groupTableView {
            
            return dataModels.count
            
        } else {
            
            let temp = rightTabelModel?.child[section].items.count ?? 0
            
            return temp
            
            
        }
    }
    
    
    
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let tableCell : UITableViewCell!
        
        if tableView == groupTableView {
            
            let temp = tableView.dequeueReusableCell(withIdentifier: LeftTableViewCell.identifer()) as! LeftTableViewCell
            
            temp.name = dataModels[indexPath.row].gc_name
            
            tableCell = temp
            
        } else {
            
            let temp = tableView.dequeueReusableCell(withIdentifier: RightTableViewCell.identifer()) as! RightTableViewCell
            
            temp.models = rightTabelModel?.child[indexPath.section].items[indexPath.row]
            
            temp.delegate = self
            
            
            tableCell = temp
        }
        
        
        return tableCell
        
    }
    
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        if tableView == groupTableView {
            
            rightTabelModel = dataModels[indexPath.row]
            
            plainTableView.reloadData()
            
            plainTableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: true)
            
            tableView.scrollToRow(at: indexPath, at: .middle, animated: true)
            
        }
        
    }
    
    
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        
        
        if tableView == groupTableView {
            
            return 0.00001
            
        } else {
            return 0.00001
            
        }
        
    }
    
    
    
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        
        
        if tableView == groupTableView {
            
            return 0.00001
            
        } else {
            return 36
            
        }
        
    }
    
    

    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        if tableView == groupTableView {
            
            return UIView()
            
        } else {
            
            
            let temp = tableView.dequeueReusableHeaderFooterView(withIdentifier: RightTabelHeaderView.identifer()) as! RightTabelHeaderView
            
            
            temp.labelText = rightTabelModel?.child[section].gc_name
            
            
            return temp
        }
        
        
        
        
        
    }


}


extension GoodsClassifyController : RightTableViewCellDeleage{
    
    func rightTableViewCellBtnDidTap(model: OneItemModel) {
        
        let temp = GoodsListController()
        
        QL1(model.gc_id);
        
        temp.goodId = model.gc_id
        
        navigationController?.pushViewController(temp, animated: true)
        
    }

 

}
